package com.itheima.d4_tcp1;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        //1.创建ServerSocket对象，准备接收数据
        ServerSocket ss = new ServerSocket(8050);
        //2.调用accept方法，获取数据
        System.out.println("等待客户端连接...");
        Socket socket = ss.accept();
        System.out.println("客户端连接成功！");

        //3.接收输入流对象

        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1){
            System.out.println(new String(bytes,0,len));
        }

        //4.回写信息
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        bos.write("滚蛋".getBytes());
        bos.flush();



        socket.close();


    }
}
